package com.lg.system.wxmaterial.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.lg.common.base.BaseController;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Insert;
import com.lg.common.validate.Update;
import com.lg.system.wxmaterial.entity.WxMaterial;
import com.lg.system.wxmaterial.service.WxMaterialService;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;

import me.chanjar.weixin.common.exception.WxErrorException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dl
 * @since 2018-04-08
 */
@RestController
@RequestMapping("/wxmaterial")
public class WxMaterialController extends BaseController<WxMaterial, WxMaterialService> {

    private final String prefix="system/wxmaterial/";
    
	@Autowired
	WxMaterialService wxMaterialService;

	@RequiresPermissions("wxmaterial:wxmaterial")
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@RequiresPermissions("wxmaterial:wxmaterial")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody WxMaterial  wxMaterial){
		Query<WxMaterial> query = new Query<WxMaterial>(wxMaterial);
		List<WxMaterial> wxMaterials=wxMaterialService.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(wxMaterials,query.getPage().getTotal());
		return pa;
	}
	
	@RequiresPermissions("wxmaterial:add")
	@GetMapping("/add")
	public ModelAndView add(){
		ModelAndView mView=new ModelAndView(prefix+"add");
		return mView;
	}
	
	@RequiresPermissions("wxmaterial:edit")
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Long id){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		mView.addObject("obj", wxMaterialService.selectById(id));
		return mView;
	}
	
	@GetMapping("/upload/{type}")
	public ModelAndView upload(@PathVariable("type") String type){
		ModelAndView mView=new ModelAndView(prefix+"upload_"+type);
		mView.addObject("type", type);
		return mView;
	}
	
	@RequiresPermissions("wxmaterial:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) WxMaterial wxMaterial,BindingResult bindingResult){
		wxMaterial.setCreateId(getUserId());
		if(wxMaterialService.save(wxMaterial)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("wxmaterial:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) WxMaterial wxMaterial,BindingResult bindingResult){
		wxMaterial.setModifiedId(getUserId());
		if(wxMaterialService.update(wxMaterial)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("wxmaterial:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) WxMaterial wxMaterial,BindingResult bindingResult){
		
		if(wxMaterialService.deleteById(wxMaterial.getId())){
			return R.ok();
		}else{
			return R.error();
		}  
	}
	
	@RequiresPermissions("wxmaterial:del")
	@PostMapping("/deleteRows")
	public R deleteRows(@RequestParam(value="ids[]") List<Long> ids){
		if(wxMaterialService.deleteByIds(ids)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@ResponseBody
	@PostMapping("/upload/{type}")
	R upload(@RequestParam("file") MultipartFile files,@PathVariable("type") String  type ,HttpServletRequest request) throws IOException, WxErrorException {
		WxMaterial wxMaterial=new WxMaterial();
		String title= request.getParameter("title")==null?"":request.getParameter("title");
		String desc=request.getParameter("desc")==null?"":request.getParameter("desc");
		wxMaterial.setTitle(title);
		wxMaterial.setDesc(desc);
		wxMaterialService.upload(files,type,wxMaterial);
		Map<String, Object> res=new HashMap<>();
		return R.ok(res);
	}
	
	
	
	
	/** 
	* @Description: 上传图文消息的banner
	* @param @param file
	* @param @param request
	* @param @return
	* @param @throws IOException
	* @param @throws WxErrorException  
	* @return R 
	* @throws 
	*/ 
	@ResponseBody
	@PostMapping("/uploadBanner")
	R uploadArticle(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws IOException, WxErrorException {
		return wxMaterialService.mediaImgUpload(file);
	}

}
